import os

from config.setting import SETTINGS_YAML_PATH, DATA_YAML_PATH
from lib.http_cli import HttpCli, log
from lib.yaml_parse import YamlParser
from utils.get_encrypt_utils import encrypt_xor_param


def call_case(yaml_path=None):
    """闭包函数解决传入yaml文件的路径并执行函数"""

    def middle(func):
        def wrapper(*args, **kwargs):
            func_name = getattr(func, "__name__")
            if os.path.exists(yaml_path) is False:
                log.info("没有这个文件路径")
            case_obj = CaseFileRequest(yaml_path)
            response = case_obj.run()
            return func(*args,**kwargs)
        return wrapper
    return middle


class ConfigParser(YamlParser):
    """配置文件解析器"""

    def __init__(self):
        YamlParser.__init__(self, SETTINGS_YAML_PATH)

    def market_base_url(self):
        """获取market域-url"""
        return self.get_yaml_data().host.market


class CaseFileRequest(YamlParser):
    """用例数据解析器以及运行"""

    def __init__(self, data_yaml_dir):
        """

        :param data_yaml_dir: 测试用例yaml文件的路径
        """
        YamlParser.__init__(self, data_yaml_dir)

    def get_case(self):
        """获取测试用例的名称"""
        case_name = self.get_yaml_data().tests.case
        return case_name

    def get_case_method(self):
        """获取接口api的调用方法"""
        case_method = self.get_yaml_data().tests.method
        return case_method

    def get_case_path(self):
        """获取测试用例数据api接口的路径"""
        case_path = self.get_yaml_data().tests.path
        return case_path

    def get_case_headers(self):
        """湖区测试用例的api接口的请求头"""
        case_headers = self.get_yaml_data().tests.headers
        return case_headers

    def get_case_params(self):
        """获取测试用例数据api接口的请求参数"""
        case_params = self.get_yaml_data().tests.params
        return case_params

    def get_params_data(self):
        """获取参数测试用例参数数据合集"""
        params_data = self.get_case_params()
        story_data = []
        for item in params_data:
            story_data.append(params_data[item])
            return story_data

    def get_case_suite(self):
        """获取测试用例的测试套件名称"""
        case_suite = self.get_yaml_data().case_suite
        return case_suite

    def get_case_description(self):
        """获取测试用例的功能描述"""
        case_description = self.get_yaml_data().description
        return case_description

    def get_case_module_class(self):
        """获取测试用例的类名称"""
        case_module_class = self.get_yaml_data().modules.module_class
        return case_module_class

    def get_case_feature(self):
        """获取测hi用力的功能介绍"""
        case_feature = self.get_yaml_data().modules.feature
        return case_feature

    def get_domain(self):
        """获取api接口加密域"""
        domain = self.get_yaml_data().tsets.domain
        return domain

    def get_sign_type(self):
        """获取api接口加密的系统类型"""
        sign_type = self.get_yaml_data().tests.signType
        return sign_type

    def run(self):
        self.host = ConfigParser().market_base_url()
        self.case_path = self.get_case_path()
        self.method = self.get_case_method()
        self.headers = self.get_case_headers()
        self.requests_params = self.get_case_params()
        self.requests_data = encrypt_xor_param(self.requests_params,
                                               self.get_domain(),
                                               self.get_case_suite())
        return HttpCli(self.headers).exec_request(method=self.method,
                                                  url=self.host+self.case_path,
                                                  data=self.requests_data
                                                  )

path = r'E:\msshuo-test\data\single\test_apply_join.yaml'
print(CaseFileRequest(path).get_case_suite())